// pages/componet/dateSearch/dateSearch.js
const app = getApp();
const dateTimePicker = require('../../../utils/dateTimePicker.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      start_date:'',
      end_date:'',
      projects:[],
      totalProject:'',
      page:1,
      all_areas:'',
      all_suits:'',
      isHideLoadMore: true,
      showModalStatus: false,
      dateTimeArray: null,
      dateTime: null,
      dateTimeArray1: null,
      dateTime1: null,
      startYear: 2000,
      endYear: '2050',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let _that = this;
    app.getToken().then(function (){
      _that.loadData();
    });
  },

  loadData: function (){
    const that = this;
    wx.setStorageSync('options_data', options);
    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: app.globalData.requestUrl + 'api/project/all',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
        start_date: options.start_date,
        end_date:options.end_date
      },
      success: function (res) {
        let temp_project = res.data.data.projects.data,
          area = 0,
          suit = 0;

        //统计每个项目向所有产品的面积和套数
        for (let value of temp_project) {

          for (var item of value.products) {
            // console.log(item.area)
            area += item.area;
            suit += item.tao;
            value['area'] = area;
            value['suit'] = suit;
          }
        }

        wx.hideLoading()
        // 执行成功
        that.setData({
          projects: res.data.data.projects.data,
          totalProject: res.data.data.projects.total,
          page: res.data.data.projects.current_page,
          all_areas: res.data.data.tongji.all_area,
          all_suits: res.data.data.tongji.all_tao,
          start_date: options.start_date.substring(0,10),
          end_date: options.end_date.substring(0,10)
        })

      }, fail: function () {

        wx.showToast({
          title: '系统错误请稍后重试',
          icon: 'none'
        });
      }
    });

    // 获取完整的年月日 时分秒，以及默认显示的数组
    let obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    let obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    let lastArray = obj1.dateTimeArray.pop();
    let lastTime = obj1.dateTime.pop();
    let end_date = obj1.dateTimeArray[0][obj1.dateTime[0]] + '-' + obj1.dateTimeArray[1][obj1.dateTime[1]] + '-' + obj1.dateTimeArray[2][obj1.dateTime[2]];
    let start_date = obj1.dateTimeArray[0][obj1.dateTime[0]] + '-' + obj1.dateTimeArray[1][obj1.dateTime[1]] + '-' + obj1.dateTimeArray[2][obj1.dateTime[2]];

    that.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      end_date: end_date,
      start_date: start_date
    });
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var options = wx.getStorageSync('options_data');
    this.onLoad(options)
    this.setData({
      showModalStatus: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    const that = this;
    let projects = that.data.projects,
      page = that.data.page;
    wx.request({
      url: app.globalData.requestUrl + 'api/project/all',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
        start_date: that.data.start_date,
        end_date: that.data.end_date,
        page: page + 1
      },
      success: function (res) {
        wx.hideLoading()
        // 执行成功
        that.setData({
          isHideLoadMore: true,
          projects: projects.concat(res.data.data.projects.data),
          totalProject: res.data.data.projects.total,
          page: res.data.data.projects.current_page,
          all_areas: res.data.data.tongji.all_area,
          all_suits: res.data.data.tongji.all_tao
        })

      }, fail: function () {
        wx.showToast({
          title: '系统错误请稍后重试',
          icon: 'none'
        });
      }
    });

    //加载状态再次初始化
    this.setData({
      isHideLoadMore: false
    })
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //时间模糊查询 弹出层
  powerDrawer: function (e) {
    let currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    let animation = wx.createAnimation({
      duration: 200,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      if (currentStatu == "close") {
        this.setData({
          showModalStatus: false
        });
      }
    }.bind(this), 200)

    // 显示  
    if (currentStatu == "open") {
      this.setData({
        showModalStatus: true
      });
    }
  },

  //模糊查询设置开始时间
  changeDateTimeStart: function (e) {
    let arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    let start_date = e.detail.value;

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr,
      start_date: start_date
    });

  },

  //模糊查询设置结束时间
  changeDateTimeEnd: function (e) {
    let arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    let end_date = e.detail.value;

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr,
      end_date: end_date
    });

  },

  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
  },


  //选择时间后确定
  sureDate: function () {
    const that = this;

    let start_date = this.data.start_date + ' ' + '00:00:00',
      end_date = this.data.end_date + ' ' + '23:59:59';
    // console.log((new Date(start_date) , new Date(end_date)))

    if ((start_date > end_date) || (start_date == end_date)) {

      wx.showToast({
        title: '结束时间必须大于开始时间',
        icon: 'none'
      });
      return;
    }
    
    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: app.globalData.requestUrl + 'api/project/all',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
        start_date: start_date,
        end_date: end_date
      },
      success: function (res) {
        let temp_project = res.data.data.projects.data,
          area = 0,
          suit = 0;

        //统计每个项目向所有产品的面积和套数
        for (let value of temp_project) {

          for (var item of value.products) {
            // console.log(item.area)
            area += item.area;
            suit += item.tao;
            value['area'] = area;
            value['suit'] = suit;
          }
        }

        wx.hideLoading()
        // 执行成功
        that.setData({
          projects: res.data.data.projects.data,
          totalProject: res.data.data.projects.total,
          page: res.data.data.projects.current_page,
          all_areas: res.data.data.tongji.all_area,
          all_suits: res.data.data.tongji.all_tao,
          start_date: start_date.substring(0, 10),
          end_date: end_date.substring(0, 10)
        })

      }, fail: function () {

        wx.showToast({
          title: '系统错误请稍后重试',
          icon: 'none'
        });
      }
    });

    that.setData({
      showModalStatus: false
    });

  },
})